from django.shortcuts import render
from django.http.response import JsonResponse
from django.utils.decorators import method_decorator
from django.views.decorators.csrf import csrf_exempt
from django.views import View
from web import models
from web.utils.tencent.cos import createBucket

from web.forms.project import ProjectForm
import uuid

class ProjectView(View):
    '''
    项目视图
    '''

    def get(self, request):
        # print(request.tracer.price_policy.title)
        # print(request.tracer.user.username)
        print(request.tracer.price_policy)
        # 获取项目的颜色种类
        colors = models.Project.color_choices
        form = ProjectForm(request=request)
        #获取项目列表
        project_dict={'star':[],'my':[],'join':[]}
        my_project_list=models.Project.objects.filter(creator=request.tracer.user)
        join_project_list=models.ProjectUser.objects.filter(user=request.tracer.user)
        for item in my_project_list:
            if item.star:
                project_dict['star'].append({'type':'my','value':item})
            else:
                project_dict['my'].append(item)
        for item in join_project_list:
            if item.star:
                project_dict['star'].append({'type':'join','value':item.project})
            else:
                project_dict['join'].append(item.project)
        return render(request, 'project/project_list.html', {'colors': colors, 'form': form,'project_dict':project_dict})

    def post(self, request):
        # print(request.POST)
        form=ProjectForm(request=request,data=request.POST)
        if form.is_valid():

            try:
                # 为项目创建存储桶
                name=str(uuid.uuid1())
                buckent_name=f'{name}-1307211509'
                createBucket(name)
                form.instance.bucket=buckent_name
                form.instance.creator=request.tracer.user
                instance=form.save()
                #初始化项目的问题类型
                issue_type_list=[]
                for item in models.IssuesType.PROJECT_INIT_LIST:
                    temp=models.IssuesType(project=instance,title=item)
                    issue_type_list.append(temp)
                models.IssuesType.objects.bulk_create(issue_type_list)
                return JsonResponse({'code': 200, 'data': 'ok', 'error': ''})
            except Exception as e:
                print('添加项目失败')
                print(e)

        else:

            return JsonResponse({'code': 400, 'data': '', 'error':form.errors})



@method_decorator(csrf_exempt,name='dispatch')
class ProjectStarView(View):
    '''
    给项目星标的处理
    '''
    def post(self,request,*args,**kwargs):
        project_type=request.POST.get('type')
        id=request.POST.get('id')
        # print(project_type,type(project_type))
        # print(id, type(id))
        if project_type=='my':
            models.Project.objects.filter(id=id,creator=request.tracer.user).update(star=True)
            return JsonResponse({'code': 200, 'data':'更新成功', 'error': ''})
        elif project_type == 'join':
            pass
        return JsonResponse({'code':200,'data':'','error':''})

@method_decorator(csrf_exempt,name='dispatch')
class ProjectUnStarView(View):
    def post(self,request, * args, ** kwargs):
        project_type = request.POST.get('type')
        id = request.POST.get('id')
        # print(project_type,type(project_type))
        # print(id, type(id))


        if project_type == 'my':

            models.Project.objects.filter(id=id, creator=request.tracer.user).update(star=False)
            return JsonResponse({'code': 200, 'data': '更新成功', 'error': ''})
        elif project_type == 'join':
            pass
        return JsonResponse({'code': 200, 'data': '', 'error': ''})